//
//  ProfileEditor.swift
//  codelabs
//
//  Created by 雷雷 on 24/05/2025.
//

import SwiftUI

struct ProfileEditor: View {
    @Binding var profile:Profile
    
    // Define date picker range
    var dateRange:ClosedRange<Date>{
        let min=Calendar.current.date(byAdding: .year, value: -1,to: profile.goalDate)!
        
        let max=Calendar.current.date(byAdding: .year, value: 1 , to: profile.goalDate)!
        
        return min...max
    }
    
    var body: some View {
        List{
            
            // User name
            HStack{
                Text("User Name")
                
                Spacer()
                
                TextField("User Name",text:$profile.username)
                    .foregroundStyle(.secondary)
                    .multilineTextAlignment(.trailing)
                
            }
                        
            
            Toggle(isOn:$profile.prefersNotifications){
                Text("Enable Notifications")
            }
            
            
            Picker("Seasons Photo",selection: $profile.seasonalPhoto){
                ForEach(Profile.Season.allCases){seasonPhoto in
                    Text("\(seasonPhoto.rawValue) \(seasonPhoto)").tag(seasonPhoto)
                }
            }
            
            DatePicker(selection:$profile.goalDate,in:dateRange,displayedComponents: .date){
                Text("Gaol Date")
            }
        }
        .listStyle(.inset)
    }
}

#Preview {
    ProfileEditor(profile: .constant(.default))
}
